import TopicModule from '../../models/3.0/topic'
Page({
  data: {
    list: [],
    selected: [],
    history: [],
    newSelected:[]
  },

  getList() {
    TopicModule.list().then((res) => {
      this.setData({
        list: res
      })
    })
  },

  selectTopic(e) {
    const item = e.currentTarget.dataset.item
    const selected = this.data.selected
    if (!selected.length || selected.some((i) => i.id !== item.id)) {
      selected.push(item)
      this.setData({
        selected
      })
      wx.setStorageSync('newSelected', this.data.selected)
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      prevPage.setData({
        topic: selected
      })
    // 存放话题到本地
    }
  },

  // 显示历史记录到页面
  get(){
   const newSelected= wx.getStorageSync('newSelected')
   this.setData({
    newSelected:newSelected.map(i=>i.topicName)
   })
  },

  removeTopic(e) {
    const item = e.currentTarget.dataset.item
    const selected = this.data.selected
    console.log('fa',selected.find((i) => i.id == item.id))
    console.log('xian',selected.map(i=>i.id).join());
    TopicModule.deleteById({
      id:selected.map(i=>i.id).join()
    }).then(res=>{
    })
  },
  onLoad(){
    this.get()
  },
  onShow() {
    this.getList()
  }
})
